package ZaznaczObszar;
import javax.swing.JPanel;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Dimension;

import javax.swing.JLabel;

import java.awt.AWTException;
import java.awt.Font;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.Robot;


public class InfoPanel extends JPanel implements infoPanel_inter {
	
	lupaEx_inter ob;
	private lupaEx_inter lupaEx_inter;
	private JLabel zaczep;
	private JLabel pole;
	
	/**
	 * Create the panel.
	 */
	public InfoPanel() {
		setSize(new Dimension(100, 130));
		setOpaque(false);
		setLayout(null);
		setVisible(false);

		JPanel panel = new JPanel();
		panel.setBounds(5, 5, 90, 120);
		add(panel);
		panel.setLayout(null);
		
		JPanel dane = new JPanel();
		dane.setAlignmentX(Component.LEFT_ALIGNMENT);
		dane.setBounds(0, 0, 90, 32);
		panel.add(dane);
		dane.setLayout(null);
		
		JLabel lab = new JLabel("XY");
		lab.setBounds(5, 5, 25, 8);
		lab.setFont(new Font("Lucida Grande", Font.BOLD | Font.ITALIC, 8));
		dane.add(lab);
		
		JLabel lab1 = new JLabel("WH");
		lab1.setFont(new Font("Lucida Grande", Font.BOLD | Font.ITALIC, 8));
		lab1.setBounds(5, 15, 25, 8);
		dane.add(lab1);
		
		zaczep = new JLabel("");
		zaczep.setFont(new Font("Lucida Grande", Font.BOLD | Font.ITALIC, 9));
		zaczep.setBounds(25, 5, 59, 8);
		dane.add(zaczep);
		
		pole = new JLabel("");
		pole.setFont(new Font("Lucida Grande", Font.BOLD | Font.ITALIC, 9));
		pole.setBounds(25, 15, 59, 8);
		dane.add(pole);
		
		JPanel lupa = new JPanel();
		lupa.setBounds(3, 33, 84, 84);
		panel.add(lupa);
		lupa.setLayout(null);
		
			ob = new lupaEx();
		JPanel obraz = ob.getInstancja();
		obraz.setBackground(Color.GRAY);
		obraz.setAlignmentY(Component.TOP_ALIGNMENT);
		obraz.setAlignmentX(Component.LEFT_ALIGNMENT);
		obraz.setBounds(new Rectangle(0, 0, 84, 84));
		lupa.add(obraz);
		obraz.setLayout(null);

	}
	
	public class lupaEx extends JPanel implements lupaEx_inter{
		private Image img = null, buf= null;
		private Robot rob;
		private int width = 84;
		private int height = 84;
		Rectangle rectangle;
		
		public lupaEx(){
			super();
			setBackground(Color.GREEN);
			setAlignmentY(Component.TOP_ALIGNMENT);
			setAlignmentX(Component.LEFT_ALIGNMENT);
			setBounds(new Rectangle(0, 0, width, height));
			setLayout(null);
						
			try {
				rob = new Robot();
			} catch (AWTException e) {
				e.printStackTrace();
			}
		}
		
		@Override
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			int w2 = this.width/2;
			int h2 = this.height/2;
			if (buf == null) return;
			img = buf.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
			g.drawImage(img, 0, 0, null);
			g.drawOval(w2-5, w2-5, 10, 10);
			g.drawOval(w2-7, w2-7, 14, 14);
			g.drawLine(w2, 10, w2, height-10);
			g.drawLine(10, h2, width-10, h2);
		}

		public void capture() {
			if (rob == null) return;
			Point pt = MouseInfo.getPointerInfo().getLocation();
			rectangle = new Rectangle(pt.x-probkaKursor/2,pt.y-probkaKursor/2, probkaKursor, probkaKursor);
			try{
			buf = rob.createScreenCapture(rectangle);
			} catch (NullPointerException e){
				System.out.println(rectangle.toString());
				System.out.println(rob);
				System.out.println(buf);
				e.getStackTrace();
			}
		}

		@Override
		public JPanel getInstancja() {
			return this;
		}
	}

	public void setBoundsInfoPanel(int x, int y, int width, int height) {
		this.zaczep.setText(" " + x + " - " + y);
		this.pole.setText(" " +Math.abs(width) + " - " + Math.abs(height));
		setLoc(width, height);
		setCapture();
	}
	
	public void setLoc(int width, int height){
		int x = MouseInfo.getPointerInfo().getLocation().x;
		int y = MouseInfo.getPointerInfo().getLocation().y;
		int odsyniecieOdKursora = 15;
		if (width>getWidth() | height>getHeight()){
			
			if (width > 0 & height > 0) {
	            setLocation(x-getWidth()-odsyniecieOdKursora, y-getHeight()-odsyniecieOdKursora); }
			if (width > 0 & height < 0) {
				setLocation(x-getWidth()-odsyniecieOdKursora, y + odsyniecieOdKursora); }
			if (width < 0 & height > 0) {
	            setLocation(x+odsyniecieOdKursora, y-getHeight()-odsyniecieOdKursora); }
			if (width < 0 & height < 0) {
	            setLocation(x+odsyniecieOdKursora, y+odsyniecieOdKursora); }
		}else{
			if (width > 0 & height > 0) {
                setLocation(x-getWidth(), y); }
           if (width > 0 & height < 0) {
                setLocation(x-getWidth(), y-getHeight()); }
           if (width < 0 & height < 0) {
                setLocation(x, y-getHeight()); }
           if (width < 0 & height > 0) {
                setLocation(x, y); }
		}
			
			
			
	}
	@Override
	public void setCapture() {
		ob.capture();
	}
	@Override
	public InfoPanel getInstancja() {
		return this;
	}

}


